import type { Metadata } from 'next';
import React from 'react';
import Layout from '@/components/Layout';
import { getServerSideCategories } from '@/services/back';
import { tryParse } from '@/utils/common';
import { getCookie, getIsLogin, Logger } from '@/utils/back';

const logger = Logger('server-layout');

export const metadata: Metadata = {
  applicationName: 'DropshipZone',
  creator: 'NewAim',
  title: 'DropshipZone',
  description:
    "Take the stress out of dropshipping by partnering with Dropshipzone; Australia's leading supplier of wholesale dropshipping supplies.",
  keywords: [
    'australia dropshipping',
    'dropship suppliers Australia',
    'drop shipping suppliers',
    'drop shipping suppliers Australia',
    'drop shipping wholesalers',
    'dropshipping wholesalers Australia',
    'dropshipping suppliers Melbourne',
    'dropshipping suppliers Sydney',
    'australian suppliers for dropshipping',
    'australian wholesale dropshipping suppliers',
    'drop shipping Melbourne',
    'drop shipping perth',
    'drop shipping sydney',
  ],
  icons: {
    icon: '/static/images/favicon.ico',
    shortcut: '/static/images/favicon.ico',
    apple: '/static/images/favicon.ico',
  },
  robots: {
    index: true,
    follow: true,
    nocache: false,
  },
};

export default async function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  logger.info('layout component render..');

  const isLogin = getIsLogin();
  const token = getCookie('token');
  const userInfo = token ? tryParse(atob(token.split('.')[1])) : {};

  const catgories = await getServerSideCategories();

  return (
    <html lang="en">
      <body>
        <Layout serverData={{ catgories, isLogin, userInfo }}>
          {children}
        </Layout>
      </body>
    </html>
  );
}
